package com.cy.pj.common.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 定义分页操作工具类，在此方法内部启动分页查询
 */
public class PageUtil {
    //super只能用于取出数据,用于supue后的类及其父类(下届限定修饰符)
//    static void show(List<? super String> t){};
    //extends限制,用于extends后的类及其子类(上届限定修饰符)
//    static <T extends String>void xxx(){};
    //声明为泛型方法
    public static <T>Page<T> startPage(){

        //获取请求方法
//        ServletRequestAttributes sra=
//                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//
//        HttpServletRequest request=sra.getRequest();
        HttpServletRequest request=ServletUtil.getRequest();
        //获取请求中的分页参数对象
        String pageCurrentStr=request.getParameter("pageCurrent");
        String pageSizeStr=request.getParameter("pageSize");

        //int pageCurrent=pageCurrentStr==null||"".equals(pageCurrentStr)?1:Integer.parseInt(pageCurrentStr);//页数
        int pageCurrent= StringUtil.isEmpty(pageCurrentStr) ?1:Integer.parseInt(pageCurrentStr);//页数
        //int pageSize=pageSizeStr==null||"".equals(pageSizeStr)?10: Integer.parseInt(pageSizeStr);//显示数
        int pageSize=StringUtil.isEmpty(pageSizeStr)?10: Integer.parseInt(pageSizeStr);//显示数

        //启动分页查询
        return PageHelper.startPage(pageCurrent,pageSize);
    }

    /*public static <T>Page<T> startPage(){
        HttpServletRequest request= ServletUtil.getRequest();

        String pageCurrentStr=request.getParameter("pageCurrent");
        Integer pageCurrent=
                StringUtil.isEmpty(pageCurrentStr)?1:Integer.parseInt(pageCurrentStr);
        Integer pageSize=
                StringUtil.isEmpty(pageSizeStr)?10:Integer.parseInt(pageSizeStr);
        return PageHelper.startPage(pageCurrent,pageSize);
    }*/
}
